package com.freemyleft.left.left_app.mian.index.work.completecoures;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;

import com.freemyleft.left.left_app.mian.index.work.waitingforclass.HomeworkDelegate;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassAdapter;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassConvert;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassFiles;
import com.freemyleft.left.zapp.delegates.LeftDelegate;

import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.IError;
import com.freemyleft.left.zapp.net.callback.IFailure;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.PickerViewUtils;
import com.freemyleft.left.zapp.ui.dialog.MyPhoneDiaLog;
import com.freemyleft.left.zapp.ui.recycler.MultipleItemEntity;
import com.freemyleft.left.zapp.util.LeftLogger;
import com.freemyleft.left.zapp.util.LeftPreference;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;

/**
 * 已上课页面
 */
public class CompleteCourseDelegate extends LeftDelegate implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R2.id.recy)
    RecyclerView recyclerView;
    @BindView(R2.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private ArrayList<MultipleItemEntity> data;
    private MyPhoneDiaLog myPhoneDiaLog;
    private String token;
    private String teacherId;
    private String id;

    @Override
    public Object setLayout() {
        return R.layout.delegate_work_complete_course;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        refreshLayout.setOnRefreshListener(this);
        initData();

    }

    private void initData() {
        long time = System.currentTimeMillis();
        Date date = new Date();
        date.setTime(time);
        String strattime = PickerViewUtils.DateToStr(date);
        token = LeftPreference.getCustomAppProfile(TOKEN);
        teacherId = LeftPreference.getCustomAppProfile(TEACHER_ID);
        RestClient.builder().url("/home/teacher/work_list")
                .loader(getContext())
                .params("teacherid", teacherId)
                .params("token", token)
                .params("type", 2)
                .params("startime", strattime)
                .params("limitnum", 10)
                .params("page", 1)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        JSONObject object = JSON.parseObject(responnse);
                        if (object.getString("code").equals("1000")) {
                            Log.e("responnse", responnse.toString());
                            refreshLayout.setRefreshing(false);
                            if (data != null) {
                                data.clear();
                            }
                            data = new CopleteCourseConvert().setJsonData(responnse).convert();
                            LeftLogger.e("WaitingForClassDelegate", responnse);
                            WaitingForClassAdapter adapter = new WaitingForClassAdapter(data);
                            Log.e("WaitingForClassDelegate", data.toString());
                            LinearLayoutManager manager = new LinearLayoutManager(getContext());
                            manager.setOrientation(LinearLayoutManager.VERTICAL);
                            recyclerView.setAdapter(adapter);
                            recyclerView.setLayoutManager(manager);
                            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    MultipleItemEntity entity = (MultipleItemEntity) adapter.getData().get(position);
                                    id = entity.getField(WaitingForClassFiles.ID);
                                    if (view.getId() == R.id.tv_yuxi) {
                                        getParentDelegate().getParentDelegate().start(new HomeworkDelegate());
                                    }
                                    if (view.getId() == R.id.imag_communication) {
                                        communication();
                                    }
                                    if (view.getId() == R.id.wancheng) {
                                        Toast.makeText(_mActivity, "完成了", Toast.LENGTH_SHORT).show();
                                        CarryOut();
                                    }
                                }
                            });
                            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    getParentDelegate().getParentDelegate().start(new CourseDetailsDelegate());
                                }
                            });
                        }
                    }
                }).failure(new IFailure() {
            @Override
            public void onFailure() {
                Toast.makeText(_mActivity, "onfailure", Toast.LENGTH_SHORT).show();
            }
        }).error(new IError() {
            @Override
            public void onError(int code, String msg) {
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show();
            }
        }).buid().post();
    }
    /**
     * 完成按钮
     */
    private void CarryOut() {
        RestClient.builder()
                .url("/home/teacher/course_statu")
                .loader(getContext())
                .params("token", token)
                .params("teacherid", teacherId)
                .params("studentid", id)
                .params("type",1)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        JSONObject jsonObject = JSONObject.parseObject(responnse);
                        if (jsonObject.getString("code").equals("1000")) {
                            Toast.makeText(_mActivity, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                        } else if (jsonObject.getString("code").equals("1001")) {
                            Toast.makeText(_mActivity, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                        }
                    }
                }).buid().post();

    }

    /**
     * 点击沟通
     */
    private void communication() {
        myPhoneDiaLog = new MyPhoneDiaLog(getContext());

        myPhoneDiaLog.setYesOnclickListener("在线沟通", new MyPhoneDiaLog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                Toast.makeText(getContext(), "在线沟通", Toast.LENGTH_SHORT).show();
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.setNoOnclickListener("电话沟通", new MyPhoneDiaLog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                //应该是每个老师会给传一个电话
                Uri data = Uri.parse("tel:" + 000000000);
                intent.setData(data);
                startActivity(intent);
                Toast.makeText(getContext(), "电话沟通", Toast.LENGTH_SHORT).show();
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.setdismissOnclickListener("取消", new MyPhoneDiaLog.onDismissOnclickListener() {
            @Override
            public void onDismissClick() {
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.show();

    }

    @Override
    public void onRefresh() {
        initData();
    }
}



